package com.ljh.sale.serviceImpl;

import com.ljh.sale.entity.Sku;
import com.ljh.sale.mapper.SkuMapper;
import com.ljh.sale.redis.RedisManager;
import com.ljh.sale.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

/**
 * Created by DELL on 2018/10/9.
 */
public class SkuServiceImpl extends BaseServiceImpl<Sku> implements SkuService {
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private RedisManager redisManager;

    @Override
    protected Mapper<Sku> getMapper() {
        return skuMapper;
    }


    @Override
    public Sku findSkuBySkuId(Long skuId) {
        if(redisManager.hasKey(skuId.toString())){
            Sku sku = (Sku)redisManager.get(skuId.toString());
            System.out.println("从缓存中获取数据");
            return sku;
        }
        Sku sku = skuMapper.selectByPrimaryKey(skuId);
        if(sku!=null){
            System.out.println("插入数据到缓存中");
            redisManager.set(skuId.toString(),sku,30);
        }
        return sku;
    }
}